package com.midea.thread;


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestExecutor {
    public static void main(String[] args) {
        //ExecutorService es = Executors.newFixedThreadPool(2);
        ExecutorService es = Executors.newCachedThreadPool();
        Runnable r1 = new Runnable() {
            @Override
            public void run() {
                for (int i = 1;i<=100;i++){
                    System.out.println("###"+i);
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        Runnable r2 = new Runnable() {
            @Override
            public void run() {
                for (int i = 1;i<=100;i++){
                    System.out.println("$$$"+i);
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        Runnable r3 = new Runnable() {
            @Override
            public void run() {
                for (int i = 1;i<=100;i++){
                    System.out.println("***"+i);
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        //把Runnable对象提交给线程池，让线程池分配线程去运行
        es.submit(r1);
        es.submit(r2);
        es.submit(r3);
        //关闭线程池
        es.shutdown();
    }
}
